package controller;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.bean.Chucvu;
import model.dao.ChucvuDAO;

/**
 * Servlet implementation class ChucvuServelet
 */
@WebServlet("/ChucvuServelet")
public class ChucvuServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public ChucvuServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");

		ChucvuDAO chucvuDAO = new ChucvuDAO();		

		//---------------------------------
		if (request.getParameter("tencv") != null) {

			String tencv = request.getParameter("tencv");
			int id = chucvuDAO.getChucvuID();
			boolean them = chucvuDAO.themCV(id, tencv);

			request.setAttribute("them", them ? "true" : "false");

			RequestDispatcher rd = request
					.getRequestDispatcher("form_chucvu.jsp");
			rd.forward(request, response);
		}
		
		//-------------------------------
		else if(request.getParameter("dsChucvu") != null) {
			
			ArrayList<Chucvu> dsChucvu = chucvuDAO.dsChucvu();
			request.setAttribute("dsChucvu", dsChucvu);
			
			RequestDispatcher rd = request
					.getRequestDispatcher("ds_chucvu.jsp");
			rd.forward(request, response);
		}
		
		//----------------------------------
		else if (request.getParameter("them_ds") != null) {
	
			RequestDispatcher rd = request
					.getRequestDispatcher("form_chucvu.jsp");
			rd.forward(request, response);
		} 
		
		//-----------------------------------------
		else if(request.getParameter("sua") != null) {
			
			String id = request.getParameter("id");
			
			Chucvu chucvu = chucvuDAO.getChucvu(id);
			request.setAttribute("chucvu", chucvu);
			
			RequestDispatcher rd = request
					.getRequestDispatcher("sua_chucvu.jsp");
			rd.forward(request, response);
			
		}
		
		//-------------------------------------------
		else if (request.getParameter("tencv2") != null) {
			
			String id = request.getParameter("idcv");
			String tencv = request.getParameter("tencv2");
			
			boolean sua = chucvuDAO.capnhatChucvu(id,tencv);
			request.setAttribute("sua", sua ? "true":"false");
			
			Chucvu chucvu = chucvuDAO.getChucvu(id);
			request.setAttribute("chucvu", chucvu);
			
			RequestDispatcher rd = request
					.getRequestDispatcher("sua_chucvu.jsp");
			rd.forward(request, response);
		}
		
		//-----------------------------------------
		else if(request.getParameter("xoa") != null) {
			
			String id = request.getParameter("xoa"); //request.getParameter("id");
			boolean xoa = chucvuDAO.xoaChucvu(id);
			request.setAttribute("xoa",xoa ? "true":"false");
			
			ArrayList<Chucvu> dsChucvu = chucvuDAO.dsChucvu();
			request.setAttribute("dsChucvu", dsChucvu);
			
			RequestDispatcher rd = request
					.getRequestDispatcher("ds_chucvu.jsp");
			rd.forward(request, response);
			
		}
		//----------------------------------------------
		else if(request.getParameter("boqua") != null) {
		
			ArrayList<Chucvu> dsChucvu = chucvuDAO.dsChucvu();
			request.setAttribute("dsChucvu", dsChucvu);
			
			RequestDispatcher rd = request
					.getRequestDispatcher("ds_chucvu.jsp");
			rd.forward(request, response);
			
		}
		//---------------------------------------------
		else {
			ArrayList<Chucvu> dsChucvu = chucvuDAO.dsChucvu();
			request.setAttribute("dsChucvu", dsChucvu);
			
			RequestDispatcher rd = request
					.getRequestDispatcher("ds_chucvu.jsp");
			rd.forward(request, response);
		}

	}

}
